#include "Util.h"
#include <common/exception.h>

QImage Util::BuildQImage(const IUintSeq &data, size_t w, size_t h, IPalette &palette)
{
    auto result = QImage(QSize(w, h), QImage::Format_RGBA8888);
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j) {
            auto index = data.GetIndex(i + j * w);
            if (index >= palette.GetLength())
                throw OutOfRangeException("IndexedImage to 32BitImage, index bigger than palette length");
            auto c = palette.GetColor(index);
            result.setPixelColor(i, j, QColor(c.r, c.g, c.b, c.a));
        }
    }
    return result;
}
